package cn.itcast.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CommonConfig {

    public static final String DIRECT_EXCHANGE = "direct.exchange";
    public static final String DIRECT_ROUTE_KEY = "direct.key";
    public static final String DIRECT_QUEUE = "direct.queue";


    @Bean
    public DirectExchange directExchange() {
        //交换机持久化
        //return new DirectExchange(DIRECT_EXCHANGE);
        //return new DirectExchange(DIRECT_EXCHANGE, true, false);
        return ExchangeBuilder.directExchange(DIRECT_EXCHANGE).durable(true).build();
    }

    @Bean
    public Queue directQueue() {
        //队列持久化
        //return new Queue(DIRECT_QUEUE);
        //return new Queue(DIRECT_QUEUE,true);
        return QueueBuilder.durable(DIRECT_QUEUE).build();
    }

    @Bean
    public Binding bindDirectQueue() {
        return BindingBuilder.bind(directQueue()).to(directExchange()).with(DIRECT_ROUTE_KEY);
    }


    @Bean
    public DirectExchange ttlExchange() {
        return new DirectExchange("ttl-exchange");
    }

    @Bean
    public Queue ttlQueue() {
        return QueueBuilder.durable("ttl-queue")
                .ttl(30000)
                .deadLetterExchange("dl-exchange")
                .deadLetterRoutingKey("dl-key")
                .build();
    }


    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(ttlQueue()).to(ttlExchange()).with("ttl-key");
    }
}
